package com.idroc.coelacanth.core.storage.support;

import java.util.ArrayList;
import java.util.List;

import com.idroc.coelacanth.core.storage.StorageKey;
import com.idroc.coelacanth.core.storage.StorageValue;

/**
 * 键值工厂
 * @author yuyoo (yuyoo4j@163.com)
 * @date 2009-12-15 上午09:32:19
 */
public class KeyValues {

	/**
	 * 获取字符串存储键
	 * @param key -- key
	 * @return -- 存储键(StorageKey)
	 */
	public static StringKey newStringKey(String key) {
		
		return new StringKey(key);
	}
	
	/**
	 * 获取字符串存储键
	 * @param key -- key
	 * @return -- 存储键(StorageKey)
	 */
	public static StringKey newStringKey(byte[] key) {
		
		return new StringKey(key);
	}
	
	/**
	 * 获取字节数组存储值
	 * @param value -- value字节数组  
	 * @return -- 存储值(StorageValue)
	 */
	public static ByteArrayValue newByteArrayValue(byte[] value) {
		
		return new ByteArrayValue(value);
	}
	
	/**
	 *  获取字节数组存储值
	 * @param value -- value字符串(utf-8)
	 * @return -- 存储值(StorageValue)
	 */
	public static ByteArrayValue newByteArrayValue(String value) {
		
		return new ByteArrayValue(value);
	}
		
	/**
	 * 防止类被非法实例化
	 */
	private KeyValues() {}
	
	/**
	 * 字符串存储键
	 * @author yuyoo (yuyoo4j@163.com)
	 * @date 2009-12-15 上午10:05:34
	 */
	public static class StringKey implements StorageKey {
		
		private String k = null;
		
		private StringKey(String key) {
			
			k = key;
		}
		
		private StringKey(byte[] key) {
			
			k = new String(key);
		}
		
		public String get() {
			return k;
		}

		public int appendBytes(byte[] content) {

			throw new UnsupportedOperationException("字符串存储键 不支持此操作");
		}

		public byte[] getBytes() { 
			 
			return k.getBytes(); 
		}

		public List<Byte> listBytes() {

			List<Byte> bytes = new ArrayList<Byte>(k.length());
			for (byte b : k.getBytes()) {
				bytes.add(Byte.valueOf(b));
			}
			return bytes;
		}

		public void setBytes(byte[] content) {
			
			k = new String(content);
		}

		public long length() { 
			return k.length();
		}
		
		@Override
		public String toString() {

			StringBuilder sb = new StringBuilder();
			sb.append("StringKey[");
			sb.append("k:").append(k);
			sb.append("]");
			return sb.toString();
		}
	}
	
	/**
	 * 字节数组存储值
	 * @author yuyoo (yuyoo4j@163.com)
	 * @date 2009-12-15 上午10:05:48
	 */
	public static class ByteArrayValue implements StorageValue {
		
		private byte[] val = null;
		
		private ByteArrayValue(byte[] v) {
			this.val = v;
		}
		
		private ByteArrayValue(String v) {
			this.val = v.getBytes();
		}
		
		public byte[] get() {
			return val;
		}
		
		public String getString() {
			return new String(val);
		}

		public int appendBytes(byte[] content) {
			throw new UnsupportedOperationException("字符串存储键 不支持此操作");
		}

		public byte[] getBytes() {
			return val;
		}

		public List<Byte> listBytes() {
			
			List<Byte> bytes = new ArrayList<Byte>(val.length);
			for (int i = 0; i < val.length; i++) {
				bytes.add(Byte.valueOf(val[i]));
			}
			return bytes;
		}

		public void setBytes(byte[] content) {
			
			val = content;
		}

		public long length() { 
			return val.length;
		}
		
		@Override
		public String toString() {

			StringBuilder sb = new StringBuilder();
			sb.append("ByteArrayValue[");
			sb.append("val:").append(new String(val));
			sb.append("]");
			return sb.toString();
		}
	}
}
